<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 扩展 Request 类
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: request.php 43 2012-03-09 08:24:10Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Request extends Kohana_Request
{

    /**
     * 判断是否 post 方法提交
     *
     * @return bool
     */
    public function is_post()
    {
        return $this->method() === HTTP_Request::POST;
    }

    /**
     * Redirects as the request response. If the URL does not include a
     * protocol, it will be converted into a complete URL.
     *
     *     $request->redirect($url);
     *
     * [!!] No further processing can be done after this method is called!
     *
     * @param   string   $url   Redirect location
     * @param   integer  $code  Status code: 301, 302, etc
     * @return  void
     * @uses    URL::site
     * @uses    Request::send_headers
     */
    public function redirect($url = '', $code = 302)
    {
        $referrer = $this->uri();

        if (strpos($referrer, '://') === FALSE)
        {
            $referrer = URL::site($referrer, TRUE, Kohana::$index_file);
        }

        if ($url AND strpos($url, '://') === FALSE AND ($url == '/' OR $url[0] != '/'))
        {
            // Make the URI into a URL
            $url = URL::site($url, TRUE, Kohana::$index_file);
        }

        if (($response = $this->response()) === NULL)
        {
            $response = $this->create_response();
        }

        echo $response->status($code)
            ->headers('Location', $url)
            ->headers('Referer', $referrer)
            ->send_headers()
            ->body();

        // Stop execution
        exit;
    }

}